Ext.define('Hr.controller.staff.StaffInfoController',{
    extend:'Ext.app.Controller',
    stores:['staff.SexStore','staff.EducationStore','staff.StateStore','staff.StaffInfoStore','staff.StaffInfoComboStore'],
    models:[],
    views:['staff.staffinfo.ShowPanel','staff.staffinfo.EditWindow'],
    init:function(){
		var id;
	    var data;
	    var flag;
        var photoName;
        var certName;
        var changePhoto = false;
        var changeCert = false;
        this.control({
        	'show grid':{
	            render:function(){
	                var store = Ext.getCmp('grid').store;
	                store.on('beforeload', function (store, options) {
	                    Ext.apply(store.proxy.extraParams, {'name':null,"val":null});
	                });
	                store.load();
	            },
	            selectionchange:function(model, records,obj){
	                data = records[0];
	                try{id = records[0].raw.id;}catch(e){}
	                Ext.getCmp('del').setDisabled(false);
	                Ext.getCmp('update').setDisabled(false);
	            }
        	},
            'show button[action = add]':{
                click:function(){
                    Ext.create("Hr.view.staff.staffinfo.EditWindow").show();
                    flag = true;
                }            
            },
            'show button[action = update]':{
                click:function(){
                    Ext.create("Hr.view.staff.staffinfo.EditWindow").show();
                    var form = Ext.getCmp('form').getForm();
                    form.loadRecord(data);
                    Ext.getCmp('image').setSrc("../upload/photo/"+Ext.getCmp('personPhoto').value);
                    flag = false;
                }
            },
            'show button[action = search]':{
                click:function(){
                    var name = Ext.getCmp('textfield').getValue();
                    var val = Ext.getCmp('combo').getValue();
                    var store = Ext.getCmp('grid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {'name':name,"val":val});
                    });
                    store.load();
                }
            },
            'show button[action = del]':{
                click:function(){
                    Ext.MessageBox.show({
                        title:'警告！',
                        msg:'<center>删除后数据无法恢复，<br/>确定删除？</center>',
                        buttons:Ext.Msg.YESNO,
                        icon:Ext.Msg.WARNING,
                        fn:function(is){
                            if (is=='yes') {
                                Ext.Ajax.request({
                                    url:'../staffInfoAction/del.do',
                                    params:{'id':id},
                                    success:function(){
                                        Ext.MessageBox.alert("提示","删除成功！");
                                        var store = Ext.getCmp('grid').store;
                                        store.remove(data);
                                        Ext.getCmp('del').setDisabled(true);
                                        Ext.getCmp('update').setDisabled(true);
                                    }
                                })
                            }
                        }
                    })
                }
            },
            
            //edit面板控制器
            'edit button[action = submit]':{
                click:function(obj){
                   var form = Ext.getCmp('form').getForm();
                   var window  = obj.up('edit');
                   if(form.isValid()){
                       Ext.Ajax.request({
                           url:'../staffInfoAction/addOrUpdate.do',
                           params:form.getValues(true),
                           success:function(){
                               var store = Ext.getCmp('grid').store;
                               store.load();
                               if(flag){
                                   store.add(form.getValues());
                                   Ext.MessageBox.show({
                                       title:'提示',
                                       msg:'添加成功！<br>继续添加？',
                                       buttons:Ext.Msg.YESNO,
                                       fn:function(is){
                                           if(is=='no'){
                                               window.close();
                                           }else{
                                               form.reset();
                                           }
                                       }
                                   })
                               }else{
                                   Ext.MessageBox.alert("提示",'修改成功！');
                                   window.close();
                                   store.load();
                                   Ext.getCmp('del').setDisabled(false);
                                   Ext.getCmp('update').setDisabled(false);
                               }
                           },
                           failure:function(){
                               Ext.MessageBox.show({
                                   title:'错误',
                                   msg:'<center>服务器传来一项错误！</center>',
                                   buttons:Ext.Msg.OK,
                                   icon:Ext.Msg.ERROR
                               });
                           }
                       })
                   }
                }
           },
           'edit':{
                close:function(){
                    var c =Ext.getCmp('id').value
                    var a =Ext.getCmp('personPhoto').value;
                    var b = Ext.getCmp('cert').value;
                    var d = 0;
                    if(changePhoto){
                        if (changeCert) {
                            d = 0;
                        }else{
                            d = 2
                        }
                    }else{
                        if (changeCert) {
                            d= 1
                        }else{
                            d= 3
                        }
                    }
                    Ext.Ajax.request({
                        url:'../staffInfoAction/delFile.do',
                        params:{"photo":a,"cert":b,"id":c,"flag":d}
                    });
                }
           },
           'edit fileuploadfield[id = photo]':{
                change:function(obj){
                    changePhoto = true;
                    var form =Ext.getCmp('form').getForm();
                    photoName = new Date().getTime();
                    var type = Ext.getCmp('photo').value;
                    Ext.getCmp('personPhoto').setValue(photoName+"."+type.split(".")[1]);
                    Ext.getCmp('photoName').setValue(type);
                    Ext.MessageBox.show({
                        title:'警告',
                        msg:'若已经存在文件，该操作会覆盖原文件，确定上传？',
                        buttons:Ext.Msg.YESNO,
                        icon:Ext.Msg.WARNING,
                        fn:function(obj){
                            if (obj=='yes') {
                                form.submit({
                                    url:'../staffInfoAction/photo.do',
                                    success:function(){
                                        Ext.MessageBox.alert("提示","上传成功");
                                        Ext.getCmp('image').setSrc('../upload/photo/'+photoName+"."+type.split(".")[1])
                                    },
                                    failure:function(){
                                        Ext.MessageBox.alert('提示','上传失败，<br>请确定上传文件不能大于9M');
                                    }
                                });
                            }
                        }
                    })
                }
           },
           'edit fileuploadfield[id = code]':{
                change:function(){
                    changeCert  = true;
                    var form =Ext.getCmp('form').getForm();
                    certName = new Date().getTime()+"."+Ext.getCmp('code').value.split(".")[1];
                    Ext.getCmp('cert').setValue(certName);
                    Ext.MessageBox.show({
                        title:'警高',
                        msg:'若已经存在文件，该操作会覆盖原文件，确定上传？',
                        buttons:Ext.Msg.YESNO,
                        icon:Ext.Msg.WARNING,
                        fn:function(obj){
                            if (obj=='yes') {
                                form.submit({
                                    url:'../staffInfoAction/cert.do',
                                    success:function(){
                                      Ext.getCmp('grid').store.load();
                                    },
                                    failure:function(){
                                        Ext.MessageBox.alert('提示','上传失败，<br>请确定上传文件不能大于9M');
                                    }
                                });
                            }
                        }
                    })
                }
           }
        })
    }
});